

//日期转换
Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

var String1 = 'yes';
var String2 = 'yes';
var String3 = 'yes';
var String4 = 'yes';
var String5 = 'yes';
var String6 = 'yes';

//单击指示灯显示日志详情
function IndicatorLog(e){//
    var url=$(e).attr('data-url');
    layer.open({
        type: 2,
        title: '',
        shadeClose: true,
        shade: 0.8,
        resize:false,
        maxmin: false, //开启最大化最小化按钮
        area: ['900px', '580px'],
        content: url
    });
}
var WarningWord = null;
//刷新周期从5分钟改成了15分钟，by 孙延维 20221003
setInterval('metAlarm()',300000*3);
function metAlarm(){
    var farmid = $('#selectFarm', window.parent.document).val();//获取父页面的元素
    var p = {
        "farmid":farmid
    };
    $.ajax({
        url: "../metAlarm/getNowMetAlarmForFarmid",
        data: p,
        type: "get",
        success: function (data) {
            if(data.length==0){
                WarningWord = "当前时段无灾害预警!";
            }
            if(data.length>0){
                WarningWord="当前发布了灾害预警消息，请注意防范!"
            }
        }
    })
}

var cars = $("#tgBigox").width();
var tg =-300;
function start(){
    var x=document.getElementById("warningword");
    x.innerHTML=WarningWord;
    tg--;
    if(tg<=-cars){
        tg=cars;
        document.getElementById('tgGoox').style.right =-cars+'px';
    }else{
        document.getElementById('tgGoox').style.left =tg+'px';
    }
    setTimeout(start,20);
}
onload=function(){setTimeout(start,1000)};


var i = 1;
 setInterval('refreshstatus()',500);
//刷新周期从5分钟改成了15分钟，by 孙延维 20221003
setInterval('GetrefreshstatusFor60s()',300000*3);

// setInterval(function(){
//     var d=new date();
//     if(d.getHours()==02&&d.getMinutes()==0)
//         location.reload([true]);
// },60000);

//指示灯状态获取
/**
 * 定时更新指示灯状态前做了frame界面的整体刷新，
 * 导致了界面5分钟就会到初始选中栏目，不合理，注释掉 by 孙延维02 20210328
 * @constructor
 */
function GetrefreshstatusFor60s(){
    // real();
    var thisTime = new Date().Format("yyyy-MM-dd hh:mm:ss");
    var farmid = $('#selectFarm', window.parent.document).val();//获取父页面的元素
    var p = {
        "thisTime":thisTime,
        "farmid":farmid
    };
    $.ajax({
        url : "../indicatorLightManage/getStatus",
        type : "get",
        contentType : "application/json",
        dataType : "json",
        data : p/*JSON.stringify(p)*/,//转换成字符串。后台使用了RequestBody注解
        success : function(data) {
            String1 = data.string1;
            String2 = data.string2;
            String3 = data.string3;
            String4 = data.string4;
            String5 = data.string5;
            String6 = data.string6;
        }
    });


}

//指示灯监听状态
function refreshstatus(){
    var thisTime = new Date().Format("yyyy-MM-dd hh:mm:ss");
    var farmid = $('#selectFarm', window.parent.document).val();//获取父页面的元素
    var p = {
        "thisTime":thisTime,
        "farmid":farmid
    };
    if(String1 == "yes" && i%2==1){
        $("#shuzhiSuccess").show();
        $("#shuzhiError").hide();
        $("#shuzhiNull").hide();
    }else if(String1 == "no" && i%2==1){
        $("#shuzhiError").show();
        $("#shuzhiSuccess").hide();
        $("#shuzhiNull").hide();
    }else if(i%2==0){
        $("#shuzhiNull").show();
        $("#shuzhiSuccess").hide();
        $("#shuzhiError").hide();
    }

    if(String2 == "yes" && i%2==1){
        $("#qixiangSuccess").show();
        $("#qixiangError").hide();
        $("#qixiangNull").hide();
    }else if(String2 == "no" && i%2==1){
        $("#qixiangError").show();
        $("#qixiangSuccess").hide();
        $("#qixiangNull").hide();
    }else if(i%2==0){
        $("#qixiangNull").show();
        $("#qixiangError").hide();
        $("#qixiangSuccess").hide();

    }

    if(String3 == "yes" && i%2==1){
        $("#shikuangSuccess").show();
        $("#shikuangError").hide();
        $("#shikuangNull").hide();
    }else if(String3 == "no" && i%2==1){
        $("#shikuangError").show();
        $("#shikuangSuccess").hide();
        $("#shikuangNull").hide();
    }else if(i%2==0){
        $("#shikuangError").hide();
        $("#shikuangSuccess").hide();
        $("#shikuangNull").show();
    }

    if(String4 == "yes" && i%2==1){
        $("#duanqiSuccess").show();
        $("#duanqiError").hide();
        $("#duanqiNull").hide();
    }else if(String4 == "no" && i%2==1){
        $("#duanqiError").show();
        $("#duanqiSuccess").hide();
        $("#duanqiNull").hide();
    }else if(i%2==0){
        $("#duanqiError").hide();
        $("#duanqiSuccess").hide();
        $("#duanqiNull").show();
    }

    if(String5 == "yes" && i%2==1){
        $("#chaoduanqiSuccess").show();
        $("#chaoduanqiError").hide();
        $("#chaoduanqiNull").hide();
    }else if(String5 == "no" && i%2==1){
        $("#chaoduanqiError").show();
        $("#chaoduanqiSuccess").hide();
        $("#chaoduanqiNull").hide();
    }else if(i%2==0){
        $("#chaoduanqiError").hide();
        $("#chaoduanqiSuccess").hide();
        $("#chaoduanqiNull").show();
    }

    if(String6 == "yes" && i%2==1){
        $("#shangbaoSuccess").show();
        $("#shangbaoError").hide();
        $("#shangbaoNull").hide();
    }else if(String6 == "no" && i%2==1){
        $("#shangbaoError").show();
        $("#shangbaoSuccess").hide();
        $("#shangbaoNull").hide();
    }else if(i%2==0){
        $("#shangbaoError").hide();
        $("#shangbaoSuccess").hide();
        $("#shangbaoNull").show();
    }
    i++;

    if(i == 101){
        i = 1;
    }
    /*$.ajax({
        url : "../indicatorLightManage/getStatus",
        type : "get",
        contentType : "application/json",
        dataType : "json",
        data : p/!*JSON.stringify(p)*!/,//转换成字符串。后台使用了RequestBody注解
        success : function(data) {
            if(data.string1 == "yes" && i%2==1){
                $("#shuzhiSuccess").show();
                $("#shuzhiError").hide();
                $("#shuzhiNull").hide();
            }else if(data.string1 == "no" && i%2==1){
                $("#shuzhiError").show();
                $("#shuzhiSuccess").hide();
                $("#shuzhiNull").hide();
            }else if(i%2==0){
                $("#shuzhiNull").show();
                $("#shuzhiSuccess").hide();
                $("#shuzhiError").hide();
            }

            if(data.string2 == "yes" && i%2==1){
                $("#qixiangSuccess").show();
                $("#qixiangError").hide();
                $("#qixiangNull").hide();
            }else if(data.string2 == "no" && i%2==1){
                $("#qixiangError").show();
                $("#qixiangSuccess").hide();
                $("#qixiangNull").hide();
            }else if(i%2==0){
                $("#qixiangNull").show();
                $("#qixiangError").hide();
                $("#qixiangSuccess").hide();

            }

            if(data.string3 == "yes" && i%2==1){
                $("#shikuangSuccess").show();
                $("#shikuangError").hide();
                $("#shikuangNull").hide();
            }else if(data.string3 == "no" && i%2==1){
                $("#shikuangError").show();
                $("#shikuangSuccess").hide();
                $("#shikuangNull").hide();
            }else if(i%2==0){
                $("#shikuangError").hide();
                $("#shikuangSuccess").hide();
                $("#shikuangNull").show();
            }

            if(data.string4 == "yes" && i%2==1){
                $("#duanqiSuccess").show();
                $("#duanqiError").hide();
                $("#duanqiNull").hide();
            }else if(data.string4 == "no" && i%2==1){
                $("#duanqiError").show();
                $("#duanqiSuccess").hide();
                $("#duanqiNull").hide();
            }else if(i%2==0){
                $("#duanqiError").hide();
                $("#duanqiSuccess").hide();
                $("#duanqiNull").show();
            }

            if(data.string5 == "yes" && i%2==1){
                $("#chaoduanqiSuccess").show();
                $("#chaoduanqiError").hide();
                $("#chaoduanqiNull").hide();
            }else if(data.string5 == "no" && i%2==1){
                $("#chaoduanqiError").show();
                $("#chaoduanqiSuccess").hide();
                $("#chaoduanqiNull").hide();
            }else if(i%2==0){
                $("#chaoduanqiError").hide();
                $("#chaoduanqiSuccess").hide();
                $("#chaoduanqiNull").show();
            }

            if(data.string6 == "yes" && i%2==1){
                $("#shangbaoSuccess").show();
                $("#shangbaoError").hide();
                $("#shangbaoNull").hide();
            }else if(data.string6 == "no" && i%2==1){
                $("#shangbaoError").show();
                $("#shangbaoSuccess").hide();
                $("#shangbaoNull").hide();
            }else if(i%2==0){
                $("#shangbaoError").hide();
                $("#shangbaoSuccess").hide();
                $("#shangbaoNull").show();
            }
            i++;

        }
    });*/


}

var farmId = $("#farmId").text();
var farmtype = $("#farmtype").text();
//var farmId = $("#farmId").html();
$(function(){

    $("#divFarm").show();
//左侧一级菜单
	$(".left-menu").click(function(){
        $(".wapper-left-menu ul li a").removeClass("active");
        $(this).addClass("active");

        $(".submenu").hide();
        $(this).next().show();
        $(".submenu-three").hide();
        $("#divFarm").show();

        $(".wapper-left-menu ul li a b").css("transform","rotate(0deg");
		$(this).find("b").css("transform","rotate(-90deg");
	});

    //左侧二级菜单
	$(".submenu li a").click(function(){
		$(".submenu li a").removeClass("active");
        $(this).addClass("active");
        $("#divFarm").show();
	});

    //左侧三级菜单
    $(".submenu-three li a").click(function(){
        //$(".submenu li a").removeClass("active");
        $(this).addClass("active");
        $("#divFarm").show();
    });

    //左侧二级菜单
    $(".left-menu-two").click(function(){
        //$(".wapper-left-menu ul li a").removeClass("active");
        $(this).addClass("active");

        //$(".submenu").hide();
        $(this).next().show();
        $("#divFarm").show();

        //$(".wapper-left-menu ul li a b").css("transform","rotate(0deg");
        $(this).find("b").css("transform", "rotate(-90deg");
    });

    //全部电站准确率点击
    $(".submenu-all").click(function () {
        $(this).addClass("active");
        $("#divFarm").hide();
    });
    loadFarmSelect();

    showLog();


});

function loadFarmSelect(){
    $.ajax({
        type: 'Get',
        url: '../main/farmList',
        dataType: "Json",
        async: true,
        success: function (data) {

                if (data != null )
                {
                    $.each(data, function (i, n) {
                        $("#selectFarm").append(" <option value=\"" + n.farmid + "\" ftype=\"" + n.farmtype + "\">" + n.farmname + "[" + n.farmid + "]" + "</option>");
                    })

                    if (farmId != null && farmId.length > 0)
                        $("#selectFarm").val(farmId);

                        $("#selectFarm").selectpicker('refresh');
                }


                loadMenu();

                changeFarmMenu();
                metAlarm();
                var initMenu = getQueryStringByName("menu");
                if (initMenu != null && initMenu == "sys")
                {
                    document.getElementById("mConifg").click();
                    document.getElementById("smFarm").click();
                    // $("#mConifg").trigger("click");
                    // $("#smFarm").trigger("click");
                }
                else if (farmtype == "1")
                {
                    document.getElementById("powerfore").click();
                    document.getElementById("sfdq").click();
                }
                else
                {
                    document.getElementById("powerfore").click();
                    document.getElementById("wfdq").click();
                }

            $("#selectFarm").change(function(){
                changeFarmMenu();
                metAlarm();
            });
          }
        });
   // $('#selectFarm').selectpicker();
   //  $("#selectFarm").val(farmId);

}

//根据权限加载菜单
function loadMenu()
{
    $.ajax({
        type: 'Get',
        url: '../home/getAccoutInfo',
        dataType: "Json",
        async: true,
        success: function (data) {
            if (data.roleid == "1") {  //管理员
                if ($('#selectFarm').find("option:selected").attr("ftype") == "0") {
                    $('li[rtype="sf"]').hide();
                }
                else
                {
                    $('li[rtype="wf"]').hide();
                }
            }
            else if (data.roleid == "4") //风电管理员
            {
                $('li[rtype="sys_sf"]').hide();
                $('li[rtype="sf"]').hide();
            }
            else if (data.roleid == "5") //光伏管理员
            {
                $('li[rtype="sys_wf"]').hide();
                $('li[rtype="wf"]').hide();
            }
            else if (data.roleid == "2") //能源局用户
            {
                $('li[rtype="sys"]').hide();
            }
            else if (data.roleid == "3") //单站用户
            {
                $('.back-index').hide();

                $('li[rtype="sys"]').hide();
                if (data.farmtype == "0")  //风电
                {
                    $('li[rtype="wf"]').show();
                    $('li[rtype="sf"]').hide();
                    document.getElementById("powerfore").click();
                    document.getElementById("wfdq").click();
                }
                else if (data.farmtype == "1")  //光伏
                {
                    $('li[rtype="wf"]').hide();
                    $('li[rtype="sf"]').show();
                    document.getElementById("powerfore").click();
                    document.getElementById("sfdq").click();
                }
            }
        }
    });

}

//根据电站类型更改菜单
function  changeFarmMenu() {

    if ($('#selectFarm').find("option:selected").attr("ftype") == "0") {
        $('li[rtype="wf"]').show();
        $('li[rtype="sf"]').hide();
    }
    else
    {
        $('li[rtype="wf"]').hide();
        $('li[rtype="sf"]').show();
    }
}

//点击指示灯，显示日志
function showLog(){

    $("[name='logInfo']").click(function () {
        var type = $(this).attr("value");
        var farmid = $('#selectFarm', window.parent.document).val();//获取父页面的元素
        var html = [];
        html.push('<table id = "demo" lay-filter="test" class="layui-hide"></table>')
        layui.use('table', function(){
            var table = layui.table;
            layer.open({
                type: 1,
                title: '日志详情',
                shadeClose: true,
                shade: 0.5,
                //shade: true,
                // maxmin: true, //开启最大化最小化按钮
                area: ['900px', '600px'],
                content: html.join(''),
                success: function () {
                }
            });
            //第一个实例
            table.render({
                elem: '#demo'
                ,height: 550,
                url: '../LogInfo/getAll?farmid='+farmid+'&type='+type+' '//数据接口
                ,cellMinWidth:100
                ,page: true //开启分页
                ,limits:[10,20]
                ,limit:10
                ,cols: [[ //表头
                    /*  {field: 'id', title: 'ID',  sort: true, width:50,fixed: 'left',align: 'center'}*/
                    /* ,{field: 'farmid', title: '电站ID',width:100,align: 'center'},*/
                    {field: 'message', title: '消息',width:300, sort: true,align: 'center'},
                    {field: 'time', title: '时间',width:300,sort: true,align: 'center',templet:'<div>{{ getLocalTime(d.time) }}</div>'},
                    /*{field: 'type', title: '类型',width:200, sort: true,align: 'center'},*/
                    {field: 'num', title: '成功条数',width:300,sort: true,align: 'center'},
                ]]
            });
        });
    })
}

//时间戳转时间
function getLocalTime(nS){
    return new Date(parseInt(nS)).toLocaleString().replace(/:\d{1,2}$/,' ');
}


function real(){
    window.location.reload(true);
}

